package com.microservice.core.mybatis;

import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Component;

import java.sql.DatabaseMetaData;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 获取数据库版本,用于判断数据库版本,mysql5.0版本某些函数语法不支持，8.0才支持，当你的生产环境和开发环境mysql数据库版本不一致时
 * 可能会需要用到此判断
 */
@Component
public class DatabaseVersionChecker {

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    public String getDatabaseVersion() throws SQLException {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            Connection connection = sqlSession.getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            return metaData.getDatabaseProductVersion();
        }
    }
}
